<template>
  <div>
    <div style="margin-bottom:20px">
        <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>视频管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>

  <el-col span="4">
    <el-input
      placeholder="请输入内容"
      v-model.trim="searchMsg"
      v-on:input="fetchVideo">
      <i slot="prefix" class="el-input__icon el-icon-search" id="search_input"></i>
    </el-input>
  </el-col>

  <el-table
    stripe
    v-loading="loading"
    ref="multipleTable"
    :data="videos"
    tooltip-effect="dark"
    style="width: 100%"
    height="550"
    @selection-change="handleSelectionChange"
    >
    <el-table-column
      type="selection"
      width="55">
    </el-table-column>
    <el-table-column
      prop="id"
      label="id"
      sortable
      width="120">
      <!-- <template slot-scope="scope">{{ scope.row.id }}</template> -->
    </el-table-column>
    <el-table-column
      prop="title"
      label="标题"
      sortable
      width="120">
    </el-table-column>
    <el-table-column
      prop="address"
      label="视频地址"
      show-overflow-tooltip>
    </el-table-column>
    <el-table-column
      prop="time"
      label="上传时间"
      show-overflow-tooltip>
    </el-table-column>

    <el-table-column
        fixed="right"
        label="操作"
        width="150">
        <template slot-scope="scope">
            <el-button @click="deleteVideo(scope.row)" type="danger" size="mini" >删除</el-button>
            <el-button size="mini" @click="editButtonClick(scope.row)">编辑</el-button>
        </template>
    </el-table-column>
  </el-table>
<el-row style="margin-top:20px">
    <el-col :span="2">
          <el-button type="danger" size="small" :disabled="multipleSelection.length==0? true:false" @click="deleteSelect()">删除所选</el-button>
    </el-col>
    <el-col :span="2">
        <videoCreate></videoCreate>
    </el-col>
    <el-col :span="2">
        <el-button type="success" size="small" @click="getVideoList()">刷新</el-button>
    </el-col>
</el-row>
<el-row>
  <div class="block">
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</el-row>

 <el-dialog title="修改视频" :visible.sync="dialogFormVisible">
        <el-form :model="selectedVideo">
          <el-form-item label="标题" :label-width="formLabelWidth">
            <el-input v-model="selectedVideo.title" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="地址" :label-width="formLabelWidth">
            <el-input v-model="selectedVideo.address" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="editVideo()">确 定</el-button>
        </div>
      </el-dialog>


</div>
</template>

<script>

import videoCreate from './videoCreate.vue'
export default {
    name:"videoList",
    components:{videoCreate},
    data(){
        return {
            loading:true,
            dialogFormVisible: false,
            multipleSelection: [],
            videos:[],
            selectedVideo:{},
            searchMsg:'',
            currentPage:1,
            total:0
        }
    },

    created(){
        this.getVideoList()
    },

    methods:{
        getVideoList(){
          this.loading = true
            this.$axios.get('/listVideoBySearchMsg',{
                params:{
                    currentPage:this.currentPage,
                    searchMsg:this.searchMsg,
                }
            }).then(resp =>{
                if(resp && resp.status === 200){
                    console.log(resp.data)
                    this.videos = resp.data.data
                    this.total = resp.data.total
                    this.loading = false
                }
            })
        },
        
        fetchVideo(){
          this.currentPage = 1
          clearTimeout(this.timer);
          this.timer=setTimeout(()=>{
            this.getVideoList()
          },800);
        },

        
        handleCurrentChange(val) {
            if(this.currentPage!=val){
              this.currentPage = val;
              this.getVideoList()
            }
        },

        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log(this.multipleSelection);
        },

        deleteVideo(video){
          this.$axios.get('/deleteVideo?id='+video.id).then(resp=>{
            if(resp.status==200){
              this.$message({message: '删除成功', type: 'success'});
              this.getVideoList()
            }
          })
        },
        editButtonClick(video){
          this.selectedVideo = video
          this.dialogFormVisible = true
        },
        editVideo(){
          this.$axios.post('/editVideo',this.selectedVideo).then(resp=>{
            if(resp.status==200){
                this.$message({message: '修改成功', type: 'success'});
              this.getVideoList()
              this.dialogFormVisible = false
            }
          })
        },
        deleteSelect(){
          for(let item of this.multipleSelection){
            this.deleteVideo(item)
          }
        }
    }
}
</script>

<style>

</style>